<template>
  <div class="login-view">
    <div class="box">
      <span class="title">猫眼电影</span>
      <i> <button class="btn">&lt;</button> </i>
    </div>
    <van-form class="v-form">
      <van-field
        class="firstinp"
        v-model="username"
        name="用户名"
        placeholder="请输入账号或手机号"
        maxlength="11"
        :rules="[{ required: true, message: '请填写账号或者手机号' }]"
      >
      </van-field>

      <van-field
        v-model="password"
        type="password"
        name="密码"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          color="#DF2D2D"
          native-type="submit"
          :loading="loading"
          @click="fn"
          >登录</van-button
        >
      </div>
      <h6 class="toregister">
        <router-link to="/register" class="toregister"
          >没有账号？点我注册>>>>></router-link
        >
      </h6>
    </van-form>
    <van-checkbox
      v-model="checked"
      checked-color="#ee0a24"
      icon-size="15px"
      :label-disabled="true"
      class="chekbox"
    >
      <span>我已阅读并同意 <i> 《猫眼用户服务协议》《隐私政策》</i> </span>
    </van-checkbox>
    <p>客服电话 : <span>1010-5335</span></p>
  </div>
</template>

<script>
import { Form, Field, Button, Checkbox, Toast } from "vant";
export default {
  name: "LoginView",
  data() {
    return {
      username: "",
      password: "",
      checked: true,
      loading: false,
    };
  },
  methods: {
    async fn() {
      if (!(this.checked && this.username && this.password)) {
        Toast.fail("请确认输入账号密码以及勾选协议");
        return;
      }
      // 调用了 vuex 的动作
      try {
        this.loading = true;

        await this.$store.dispatch("login", {
          username: this.username,
          password: this.password,
        });

        // 3. 跳转到 首页 或者 之前要去的页面
        const redirectUrl = this.$route.query.redirect || "/";
        this.$router.replace(redirectUrl);
      } catch (error) {
        console.log(error);
        Toast(error.message);
      } finally {
        this.loading = false;
      }
    },
  },
  components: {
    [Form.name]: Form,
    [Field.name]: Field,
    [Button.name]: Button,
    [Checkbox.name]: Checkbox,
    [Toast.name]: Toast,
  },
};
</script>
<style scoped lang="scss">
.login-view {
  height: 100%;
  display: flex;
  flex: 1;
  flex-direction: column;
  .box {
    height: 50px;
    background-color: #e54847;
    text-align: center;
    line-height: 50px;
    position: relative;
    .title {
      color: #fff;
      font-size: 18px;
    }
    i {
      position: absolute;
      left: 10px;
      color: #fff;
      font-size: 20px;
      .btn {
        background-color: #e54847;
        border: none;
      }
    }
  }
  .v-form {
    width: 100%;
    height: 250px;
    .firstinp {
      margin-top: 20px;
      .btn {
        border: none;
        color: #df2d2d;
      }
    }
  }
  .chekbox {
    margin-left: 20px;
    span {
      font-size: 12px;
      color: #00000080;
    }
    i {
      font-style: normal;
      color: #df2d2d;
    }
  }
  p {
    font-size: 10px;
    color: #00000080;
    text-align: center;
    line-height: 10px;
    span {
      color: #df2d2d;
    }
  }
  .prompt-text {
    font-size: 12px;
    color: #999999;
    text-align: center;
    line-height: 40px;
  }
  .toregister {
    color: #df2d2d;
    text-align: center;
    margin: 2px;
  }
}
</style>
